#include <iostream>
#include <string>
#include <stack>
using namespace std;

bool isDigit(char c) {
    return (c >= '0' && c <= '9');
}

int precedence(char op) {
    switch (op) {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    default:
        return -1;
    }
}

float applyOperation(float a, float b, char op) {
    switch (op) {
    case '+':
        return a + b;
    case '-':
        return a - b;
    case '*':
        return a * b;
    case '/':
        return a / b;
    default:
        return 0;
    }
}

void evaluatecalculationFormula() {
    stack<float> numbers;
    stack<char> operators;
    string calculationFormula;
    cout << "请输入所要计算的计算式: "<<endl;
    getline(cin, calculationFormula); // 读取整行输入，包括空格
    for (int i = 0; i < calculationFormula.length(); i++) {
        if (calculationFormula[i] == ' ') continue;
        if (calculationFormula[i] == '=') continue;
        if (isDigit(calculationFormula[i])) {
            float num = calculationFormula[i] - '0';
            while (i + 1 < calculationFormula.length() && isDigit(calculationFormula[i + 1])) {
                num = num * 10 + (calculationFormula[i + 1] - '0');
                i++;
            }
            numbers.push(num);
        }
        else {
            if (operators.empty() || calculationFormula[i] == '(') {
                operators.push(calculationFormula[i]);
                continue;
            }
            while (!operators.empty() && precedence(calculationFormula[i]) <= precedence(operators.top())) {
                float b = numbers.top();
                numbers.pop();
                float a = numbers.top();
                numbers.pop();
                char op = operators.top();
                operators.pop();
                numbers.push(applyOperation(a, b, op));
            }
            operators.push(calculationFormula[i]);
        }
    }
    while (!operators.empty()) {
        float b = numbers.top();
        numbers.pop();
        float a = numbers.top();
        numbers.pop();
        char op = operators.top();
        operators.pop();
        numbers.push(applyOperation(a, b, op));
    }
    float result = numbers.top();
    cout << "结果为: " << result << endl;
}


int main() {
    evaluatecalculationFormula();
    return 0;
}
